# -*- coding: utf-8 -*-

#Author: Yuanhao Li <jay_21cn@hotmail.com>
#File: view_ui.py
#Usage: Client of ING from project Babel
#Copyright (C) 2007 Yuanhao Li<jay_21cn@hotmail.com>

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software Foundation,
#Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#本程序是遵从 GPL 2.0 许可发布的. 你可以自由修改和再发布本程序, 
#但请遵守 GPL 2.0 条款, 尊重知识产权.

import sys, os, urllib, md5

from PyQt4 import QtCore, QtGui

cwd = os.path.split(sys.argv[0])[0]
if not cwd:
	cwd = os.getcwd()

class IngcBrowser(QtGui.QTextBrowser):
	def loadResource(self, typ, name):
		url = unicode(name.toString())
		ret = QtCore.QVariant()
		if url.startswith('http://'):
			dn = os.path.join(cwd, 'cache')
			if not os.path.isdir(dn):
				os.mkdir(dn)
			m = md5.new()
			m.update(url)
			fn = os.path.join(cwd, 'cache', m.hexdigest())
			#fn = 'cache/' + m.hexdigest()
			try:
				if not os.path.isfile(fn):
					urllib.urlretrieve(url, fn)
				ret = QtGui.QTextBrowser.loadResource(self, typ, QtCore.QUrl('cache/' + m.hexdigest()))
			except IOError:
				ret = QtGui.QTextBrowser.loadResource(self, typ, QtCore.QUrl(os.path.join(cwd, 'pixmaps', 'picture_error.png')))
		else:
			try:
				ret = QtGui.QTextBrowser.loadResource(self, typ, name)
			except IOError:
				ret = QtGui.QTextBrowser.loadResource(self, typ, QtCore.QUrl(os.path.join(cwd, 'pixmaps', 'picture_error.png')))
		return ret

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("INGC Tab View")
        Form.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(Form.minimumSizeHint()))

        self.vboxlayout = QtGui.QVBoxLayout(Form)
        self.vboxlayout.setMargin(0)
        self.vboxlayout.setSpacing(0)
        self.vboxlayout.setObjectName("vboxlayout")

        self.textBrowser = IngcBrowser(Form)
        self.textBrowser.setObjectName("textBrowser")
        self.textBrowser.setOpenExternalLinks(True)
        self.vboxlayout.addWidget(self.textBrowser)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))

